package com.lh.java.basic.fileread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;

public class TestHelloBeanReadProperyConfigue {

	public void read(String path) {
		try {			
			//InputStream in = this.getClass().getClassLoader()
				//	.getResourceAsStream(path);	
			ClassPathResource resource = new ClassPathResource(path);
			File f = resource.getFile();
			FileInputStream in =  new FileInputStream(f);
			//InputStream in = new BufferedInputStream(new FileInputStream(path));
			Properties p = new Properties();
			p.load(in);		
			// p.list(System.out);

			System.out.println(p.getProperty("ip") + ","
					+ p.getProperty("username") + "," + p.getProperty("pwd"));
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void update(String path) {
		try {
			Properties p = new Properties();
			ClassPathResource resource = new ClassPathResource(path);
			File f = resource.getFile();
			FileInputStream in =  new FileInputStream(f);
			p.load(in);
			FileOutputStream out = new FileOutputStream(f);

			p.setProperty("ip", "sfsdf");
			p.store(out, "up");
			// p.save(out,"ip updated");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String fileName = "lh/ssi/readconfigurefile/beanConfig.properties";
		String fileName1 = "beanConfig.properties";
		TestHelloBeanReadProperyConfigue td = new TestHelloBeanReadProperyConfigue();
		td.read(fileName1);
		td.update(fileName1);
		td.read(fileName1);
	}
	// 1.使用java.util.Properties的load()方法

	// System.out.println("helloBean.class:"
	// + p.getProperty("helloBean.class")
	// + ",helloBean.helloWord:"
	// + p.getProperty("helloBean.helloWord"));

}
